home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / devel201.zip / ST-WND-M.C < prev   
C/C++ Source or Header  |  1995-02-19  |  19KB  |  474 lines

  1. /**************************************************************************************************
  2.  
  3.     SPELTEST
  4.     Program to test spelling checker.
  5.  
  6.     Written and copyright by Brian J Quinion 1995.
  7.  
  8.     Version for Microsoft Windows.
  9.     Version:  0.01 (Production)
  10.     Date:     19 Feb 1995
  11.     Module:   ST-WND-M.C
  12.  
  13. **************************************************************************************************/
  14.  
  15.     /* include files common to all modules */
  16. #define EXTERN extern
  17.     #include "speltest.h"
  18.  
  19.     /* include files for this module only */
  20.     #pragma  hdrstop
  21.     /* NONE */
  22.  
  23. /***************************************************************************************************
  24.  
  25.     RegisterSPELTEST_MAIN
  26.     ---------------------
  27.     Register window class 'SPELTEST_MAIN'
  28.  
  29.     Parameters : None
  30.     Called by  : WinMain on initialisation
  31.     Calls      : Nothing
  32.     Modifies   : Nothing
  33.     Returns    : Nothing
  34.  
  35. ***************************************************************************************************/
  36. void RegisterSPELTEST_MAIN(void)
  37. {
  38.     WNDCLASS    wndclass;
  39.  
  40.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  41.     wndclass.lpfnWndProc   = (WNDPROC)SPELTEST_MAINWndProc;
  42.     wndclass.cbClsExtra    = 0;
  43.     wndclass.cbWndExtra    = 0;
  44.     wndclass.hInstance     = g.hInst;
  45.     wndclass.hIcon         = LoadIcon(g.hInst, "SPELTEST_MAIN");
  46.     wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  47.     wndclass.hbrBackground = CreateSolidBrush(RGB(0,0,0));
  48.     wndclass.lpszMenuName  = "SPELTEST_MAIN";
  49.     wndclass.lpszClassName = "SPELTEST_MAIN";
  50.     RegisterClass(&wndclass);
  51. }
  52.  
  53. /***************************************************************************************************
  54.  
  55.     SPELTEST_MAINWndProc
  56.     -----------------
  57.     To handle messages to 'SPELTEST_MAIN' class
  58.  
  59.     Parameters : WND standard
  60.     Called by  : Windows
  61.     Calls      : 
  62.     Modifies   : 
  63.     Returns    : Depends on calling parameters
  64.  
  65. ***************************************************************************************************/
  66. #pragma argsused
  67. long FAR PASCAL SPELTEST_MAINWndProc(HWND hWnd, UINT wMsg, WORD wParam, LONG lParam)
  68. {
  69.     PAINTSTRUCT            ps;
  70.  
  71. // Quick
  72.     HINSTANCE                   hModSpelledt;
  73.     BOOL (WINAPI FAR * LINK_SPEDT_CheckEdit)(HWND);
  74.     void (WINAPI FAR * LINK_SPEDT_SetupBox)(HWND);
  75.  
  76. // Full - see how much more of it there is! :-)
  77.     static HWND                 hWndEdit;
  78.     CHECKWORD                   CheckWord;
  79.     LPCUSTDIC                   lpCustDic;
  80.     char            GetCustPath[]="Path???";
  81.     char            GetCustName[]="Name???";
  82.     char            GetCustOpts[]="Opts???";
  83.     int                         Count;
  84.     HINSTANCE                   hModSpellchk;
  85.     BOOL (WINAPI FAR * LINK_SPCHK_CheckWord)(LPCHECKWORD);
  86.     void (WINAPI FAR * LINK_SPCHK_Options)(LPCHECKWORD);
  87.  
  88.     static char                 Words[6][15]={"thiss",
  89.                                             "poeple",
  90.                                             "aggrivation",
  91.                                             "enviromental",
  92.                                             "wheight",
  93.                                             "nohting"};
  94.     LPCHECKWORD                 lpchkw;
  95.     WORD                        wIndex;
  96.     char                        Text[100];
  97.     static BOOL                 bCanUndo;
  98.     static HUNDO                hUndoHandle;
  99.     static WORD                 wUndoIndex;
  100.     static char                 szPreviousWord[MAXSPELL];
  101.  
  102. // Shared
  103.     char                        FileName[MAXPATH];
  104.  
  105.     switch(wMsg)
  106.     {
  107.     case WM_CREATE:
  108.         hWndEdit=CreateWindow("EDIT", NULL, ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_NOHIDESEL | ES_OEMCONVERT | ES_WANTRETURN | WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_TABSTOP,
  109.                              0, 0, 400, 200, hWnd, -1, g.hInst, NULL);
  110.         break;
  111.         
  112.     case WM_PAINT:
  113.         // kill paint messages to stop them looping
  114.     BeginPaint(hWnd, &ps);
  115.     EndPaint(hWnd, &ps);
  116.     return 0;
  117.  
  118.     case WM_SYSCOLORCHANGE:
  119.     return 0;
  120.  
  121.     case WM_COMMAND:
  122.     switch(wParam)
  123.     {
  124. // Quick 
  125.         case MLC_CHECKEDIT:
  126.             // Load the checkedt module - done like this so that the program
  127.             // could run without having the spelledt module available
  128.             // Loading a Lib file in the project would result in an error
  129.             // if the module was no available.
  130.  
  131.             // Get the path and try to load the module
  132.             GetPrivateProfileString("Modules", "spelledt", "spelledt.dll", FileName, MAXPATH, "Spelledt.ini");
  133.             hModSpelledt=LoadLibrary(FileName);
  134.             if (hModSpelledt<HINSTANCE_ERROR)
  135.             {
  136.                 MessageBox(hWnd, "Unable to load the module 'spelledt.dll'", "Spell setup", MB_ICONSTOP);
  137.                 return 0;
  138.             }
  139.  
  140.             // Get the address of the function and call it
  141.             (FARPROC)LINK_SPEDT_CheckEdit=GetProcAddress(hModSpelledt, "SPEDT_CheckEdit");
  142.             if (LINK_SPEDT_CheckEdit==NULL)
  143.                 MessageBox(hWnd, "Unable to load the function 'SPEDT_CheckEdit', progably due to an incompatable version.  Please check your system for multiple versions of 'spelledt.dll'.", "Spell setup", MB_ICONSTOP);
  144.             else
  145.                 (LINK_SPEDT_CheckEdit)(hWndEdit); // spell check the edit box
  146.  
  147.             // Free the module and finish
  148.             FreeModule(hModSpelledt);
  149.             return 0;
  150.  
  151.         case MLC_SETUPEDIT:
  152.             // Load the checkedt module, but for a different function.
  153.  
  154.             // Get the path and try to load the module
  155.             GetPrivateProfileString("Modules", "spelledt", "spelledt.dll", FileName, MAXPATH, "Spelledt.ini");
  156.             hModSpelledt=LoadLibrary(FileName);
  157.             if (hModSpelledt<HINSTANCE_ERROR)
  158.             {
  159.                 MessageBox(hWnd, "Unable to load the module 'spelledt.dll'", "Spell setup", MB_ICONSTOP);
  160.                 return 0;
  161.             }
  162.  
  163.             // Get the address of the function and call it
  164.             (FARPROC)LINK_SPEDT_SetupBox=GetProcAddress(hModSpelledt, "SPEDT_SetupBox");
  165.             if (LINK_SPEDT_SetupBox==NULL)
  166.                 MessageBox(hWnd, "Unable to load the function 'SPEDT_SetupBox', probably due to an incompatable version.  Please check your system for multiple versions of 'spelledt.dll'.", "Spell setup", MB_ICONSTOP);
  167.             else
  168.                 (LINK_SPEDT_SetupBox)(hWnd); // spell check the edit box
  169.  
  170.             // Free the module and finish
  171.             FreeModule(hModSpelledt);
  172.             return 0;
  173.  
  174. // Full
  175.         case MLC_CHECKWORDS:
  176.             // Build the checkword block
  177.         CheckWord.wSizeOfBlock     = sizeof(CHECKWORD);
  178.         CheckWord.hWndParent       = hWnd;  // window that msg are sent to
  179.         CheckWord.hInstance        = g.hInst;
  180.         CheckWord.lpOptionsDlg     = "SPELTEST_OPTIONSBOX";
  181.         CheckWord.CheckWordOptions = CWO_ALLOWCHANGE    // Do display the box if a word not found
  182.                                        | CWO_CHECKMULTIPLE  // Send GETNEXT messages
  183.                                        | CWO_USEOPTIONSHOOK // Do use the options hook
  184.                                        | CWO_USECUSTOMOPTIONSDLG // and use our dialog box 
  185.                                        | CWO_NOHELP         // No help available
  186.                                        | CWO_UNDO           // Undo is handled
  187.                                        | CWO_AUTOSUGGEST;    // Please auto suggest
  188.         CheckWord.dwCustData  = 0; // Index of the first word to check
  189.         CheckWord.fpOptionsHook  = (DLGPROC)MakeProcInstance(DLG_OptionsBox, g.hInst);
  190.  
  191.             // Get the current language from the one selected by spelledt
  192.         GetPrivateProfileString("Options", "Language", "", CheckWord.szLanguage, 13, "spelledt.ini");
  193.  
  194.         // Load custom dictionaries, in this case we will share the ones that are used by
  195.             // spelledt (i.e. the edit box one).
  196.         CheckWord.NumCustom=GetPrivateProfileInt("Custom", "Number", 0, "spelledt.ini");
  197.         CheckWord.CurCustom=GetPrivateProfileInt("Custom", "Current", 0, "spelledt.ini");
  198.         if (CheckWord.NumCustom!=0)
  199.             {
  200.         CheckWord.hCustomDics = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(CUSTDIC)*CheckWord.NumCustom);
  201.         lpCustDic = (LPCUSTDIC)GlobalLock(CheckWord.hCustomDics);
  202.         for (Count=0; Count<CheckWord.NumCustom; Count++)
  203.             {
  204.             itoa(Count, GetCustPath+4, 10);
  205.             itoa(Count, GetCustName+4, 10);
  206.             itoa(Count, GetCustOpts+4, 10);
  207.             GetPrivateProfileString("Custom", GetCustPath, "custom.dic", (lpCustDic+Count)->DicFile, MAXPATH, "spelledt.ini");
  208.             GetPrivateProfileString("Custom", GetCustName, "Custom", (lpCustDic+Count)->DicTitle, MAXDICTITLE, "spelledt.ini");
  209.             (lpCustDic+Count)->Options=GetPrivateProfileInt("Custom", GetCustOpts, CD_CHANGED, "spelledt.ini");
  210.         }
  211.         GlobalUnlock(CheckWord.hCustomDics);
  212.             }
  213.  
  214.             // Reset the undo pointer
  215.             bCanUndo=FALSE;
  216.  
  217.             // Load the checkchk module.
  218.  
  219.             // Get the path and try to load the module
  220.             GetPrivateProfileString("Modules", "spellchk", "spellchk.dll", FileName, MAXPATH, "Spelledt.ini");
  221.             hModSpellchk=LoadLibrary(FileName);
  222.             if (hModSpellchk<HINSTANCE_ERROR)
  223.             {
  224.                 MessageBox(hWnd, "Unable to load the module 'spellchk.dll'", "Spell setup", MB_ICONSTOP);
  225.                 return 0;
  226.             }
  227.  
  228.             // Get the address of the function and call it
  229.             (FARPROC)LINK_SPCHK_CheckWord=GetProcAddress(hModSpellchk, "SPCHK_CheckWord");
  230.             if (LINK_SPCHK_CheckWord==NULL)
  231.                 MessageBox(hWnd, "Unable to load the function 'SPCHK_CheckWord', probably due to an incompatable version.  Please check your system for multiple versions of 'spellchk.dll'.", "Spell setup", MB_ICONSTOP);
  232.             else
  233.                 (LINK_SPCHK_CheckWord)(&CheckWord); // spell check the edit box
  234.  
  235.             // Free the module
  236.             FreeModule(hModSpellchk);
  237.  
  238.         // Free ProcInstance for the options hook
  239.         FreeProcInstance(CheckWord.fpOptionsHook);
  240.  
  241.             // Store the language
  242.         WritePrivateProfileString("Options", "Language", CheckWord.szLanguage, "spelledt.ini");
  243.  
  244.         // Save any changes to the custom dics
  245.         lpCustDic = (LPCUSTDIC)GlobalLock(CheckWord.hCustomDics);
  246.         WritePrivateProfileInt("Custom", "Number", CheckWord.NumCustom, "spelledt.ini");
  247.         WritePrivateProfileInt("Custom", "Current", CheckWord.CurCustom, "spelledt.ini");
  248.         for (Count=0; Count<CheckWord.NumCustom; Count++)
  249.         {
  250.         itoa(Count, GetCustPath+4, 10);
  251.         itoa(Count, GetCustName+4, 10);
  252.         itoa(Count, GetCustOpts+4, 10);
  253.         WritePrivateProfileString("Custom", GetCustPath, (lpCustDic+Count)->DicFile, "spelledt.ini");
  254.         WritePrivateProfileString("Custom", GetCustName, (lpCustDic+Count)->DicTitle, "spelledt.ini");
  255.         WritePrivateProfileInt("Custom", GetCustOpts, (lpCustDic+Count)->Options, "spelledt.ini");
  256.             }
  257.         GlobalUnlock(CheckWord.hCustomDics);
  258.         GlobalFree(CheckWord.hCustomDics);
  259.  
  260.             // finish
  261.             return 0;
  262.  
  263.         case MLC_SETUPWORDS:
  264.             // Build the checkword block
  265.         CheckWord.wSizeOfBlock     = sizeof(CHECKWORD);
  266.         CheckWord.hWndParent       = hWnd;  // window that msg are sent to
  267.         CheckWord.hInstance        = g.hInst;
  268.         CheckWord.lpOptionsDlg     = "SPELTEST_OPTIONSBOX";
  269.         CheckWord.CheckWordOptions = CWO_ALLOWCHANGE    // Do display the box if a word not found
  270.                                        | CWO_CHECKMULTIPLE  // Send GETNEXT messages
  271.                                        | CWO_USEOPTIONSHOOK // Do use the options hook
  272.                                        | CWO_USECUSTOMOPTIONSDLG // and use our dialog box
  273.                                        | CWO_NOHELP         // No help available
  274.                                     // | CWO_UNDO           // not needed
  275.                                        | CWO_AUTOSUGGEST;    // Please auto suggest
  276.         CheckWord.dwCustData  = 0; // not used (by this prog, it is still there is you want it)
  277.         CheckWord.fpOptionsHook  = (DLGPROC)MakeProcInstance(DLG_OptionsBox, g.hInst);
  278.  
  279.             // Get the current language from the one selected by spelledt
  280.         GetPrivateProfileString("Options", "Language", "", CheckWord.szLanguage, 13, "spelledt.ini");
  281.  
  282.         // Load custom dictionaries, in this case we will share the ones that are used by
  283.             // spelledt (i.e. the edit box one).
  284.         CheckWord.NumCustom=GetPrivateProfileInt("Custom", "Number", 0, "spelledt.ini");
  285.         CheckWord.CurCustom=GetPrivateProfileInt("Custom", "Current", 0, "spelledt.ini");
  286.         if (CheckWord.NumCustom!=0)
  287.             {
  288.         CheckWord.hCustomDics = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(CUSTDIC)*CheckWord.NumCustom);
  289.         lpCustDic = (LPCUSTDIC)GlobalLock(CheckWord.hCustomDics);
  290.         for (Count=0; Count<CheckWord.NumCustom; Count++)
  291.             {
  292.             itoa(Count, GetCustPath+4, 10);
  293.             itoa(Count, GetCustName+4, 10);
  294.             itoa(Count, GetCustOpts+4, 10);
  295.             GetPrivateProfileString("Custom", GetCustPath, "custom.dic", (lpCustDic+Count)->DicFile, MAXPATH, "spelledt.ini");
  296.             GetPrivateProfileString("Custom", GetCustName, "Custom", (lpCustDic+Count)->DicTitle, MAXDICTITLE, "spelledt.ini");
  297.             (lpCustDic+Count)->Options=GetPrivateProfileInt("Custom", GetCustOpts, CD_CHANGED, "spelledt.ini");
  298.         }
  299.         GlobalUnlock(CheckWord.hCustomDics);
  300.             }
  301.  
  302.             // Load the checkchk module.
  303.             // Get the path and try to load the module
  304.             GetPrivateProfileString("Modules", "spellchk", "spellchk.dll", FileName, MAXPATH, "Spelledt.ini");
  305.             hModSpellchk=LoadLibrary(FileName);
  306.             if (hModSpellchk<HINSTANCE_ERROR)
  307.             {
  308.                 MessageBox(hWnd, "Unable to load the module 'spellchk.dll'", "Spell setup", MB_ICONSTOP);
  309.                 return 0;
  310.             }
  311.  
  312.             // Get the address of the function and call it
  313.             (FARPROC)LINK_SPCHK_Options=GetProcAddress(hModSpellchk, "SPCHK_Options");
  314.             if (LINK_SPCHK_Options==NULL)
  315.                 MessageBox(hWnd, "Unable to load the function 'SPCHK_Options', probably due to an incompatable version.  Please check your system for multiple versions of 'spellchk.dll'.", "Spell setup", MB_ICONSTOP);
  316.             else
  317.                 (LINK_SPCHK_Options)(&CheckWord); // spell check the edit box
  318.  
  319.             // Free the module
  320.             FreeModule(hModSpellchk);
  321.  
  322.         // Free ProcInstance for the options hook
  323.         FreeProcInstance(CheckWord.fpOptionsHook);
  324.  
  325.             // Store the language
  326.         WritePrivateProfileString("Options", "Language", CheckWord.szLanguage, "spelledt.ini");
  327.  
  328.         // Save any changes to the custom dics
  329.         lpCustDic = (LPCUSTDIC)GlobalLock(CheckWord.hCustomDics);
  330.         WritePrivateProfileInt("Custom", "Number", CheckWord.NumCustom, "spelledt.ini");
  331.         WritePrivateProfileInt("Custom", "Current", CheckWord.CurCustom, "spelledt.ini");
  332.         for (Count=0; Count<CheckWord.NumCustom; Count++)
  333.         {
  334.         itoa(Count, GetCustPath+4, 10);
  335.         itoa(Count, GetCustName+4, 10);
  336.         itoa(Count, GetCustOpts+4, 10);
  337.         WritePrivateProfileString("Custom", GetCustPath, (lpCustDic+Count)->DicFile, "spelledt.ini");
  338.         WritePrivateProfileString("Custom", GetCustName, (lpCustDic+Count)->DicTitle, "spelledt.ini");
  339.         WritePrivateProfileInt("Custom", GetCustOpts, (lpCustDic+Count)->Options, "spelledt.ini");
  340.             }
  341.         GlobalUnlock(CheckWord.hCustomDics);
  342.         GlobalFree(CheckWord.hCustomDics);
  343.  
  344.             // finish
  345.             return 0;
  346.     }
  347.         break;
  348.  
  349.     case SPELL_GETNEXT:
  350.         // Get the pointer
  351.         lpchkw = (LPCHECKWORD)lParam;
  352.         wIndex = (WORD)lpchkw->dwCustData;
  353.  
  354.         // Have we done all the words?
  355.         if (wIndex>5)
  356.             return FALSE;
  357.  
  358.         // Copy over the word
  359.         lstrcpy(lpchkw->ToCheck, Words[wIndex]);
  360.  
  361.         // increment the count
  362.         lpchkw->dwCustData++;
  363.         return TRUE;
  364.  
  365.     case SPELL_WORDNOTFOUND:
  366.         // Get the pointer
  367.         lpchkw = (LPCHECKWORD)lParam;
  368.  
  369.         // Display the word not found
  370.         wsprintf(Text, "The word %s was not found", lpchkw->ToCheck);
  371.         MessageBox(hWnd, Text, "Test Word", MB_ICONINFORMATION);
  372.         return 0;
  373.  
  374.     case SPELL_WORDCHANGED:
  375.         // Get the pointer
  376.         lpchkw = (LPCHECKWORD)lParam;
  377.         wIndex = (WORD)lpchkw->dwCustData;
  378.  
  379.         // Change the word
  380.         lstrcpy(Words[wIndex-1], lpchkw->Changed);
  381.  
  382.         // Display the word changed
  383.         wsprintf(Text, "The word %s has been changed to %s",
  384.                    lpchkw->ToCheck, lpchkw->Changed);
  385.         MessageBox(hWnd, Text, "Test Word", MB_ICONHAND);
  386.         return 0;
  387.  
  388.     case SPELL_CANUNDO:
  389.         // Just return the bUndo flag
  390.         return bCanUndo;
  391.  
  392.     case SPELL_STOREUNDO:
  393.         // Get the pointer
  394.         lpchkw = (LPCHECKWORD)lParam;
  395.  
  396.         // Yes we can now undo
  397.         bCanUndo=TRUE;
  398.  
  399.         // Store the handle provided by spellchk
  400.         hUndoHandle=(HUNDO)wParam;
  401.  
  402.         // Store the current word being checked
  403.         wUndoIndex=lpchkw->dwCustData-1;
  404.         lstrcpy(szPreviousWord, lpchkw->ToCheck);
  405.         return 0;
  406.  
  407.     case SPELL_UNDOLAST:
  408.         // Get the pointer
  409.         lpchkw = (LPCHECKWORD)lParam;
  410.  
  411.         // Do the undo if we can - see the full example app for
  412.         // more details
  413.         if (bCanUndo)
  414.         {
  415.             // Do the undo
  416.             lstrcpy(Words[wUndoIndex], szPreviousWord);
  417.  
  418.             // Reposition the index to restart at the pervious position
  419.             lpchkw->dwCustData=wUndoIndex;
  420.  
  421.             // Store the undo handle in the dialog result register
  422.             SetWindowLong(lpchkw->hWndDlg, DWL_MSGRESULT, hUndoHandle);
  423.  
  424.             // Since we have undone once we can't do it again
  425.             bCanUndo=FALSE;
  426.         
  427.             // Return a positive answer
  428.             return FALSE;
  429.         }
  430.         return TRUE;
  431.  
  432.     case SPELL_GETCUSTOMDEFPATH:
  433.     lstrcpy((LPSTR)lParam, g.szProgDir);
  434.     return TRUE;
  435.  
  436.     case WM_CLOSE:
  437.     break;
  438.  
  439.     case WM_DESTROY:
  440.     PostQuitMessage(0);
  441.         break;
  442.     }
  443. return DefWindowProc(hWnd, wMsg, wParam, lParam);
  444. }
  445.  
  446. /***************************************************************************************************
  447.  
  448.     DLG_OptionsBox
  449.     --------------
  450.     Function to handle hooking the options box
  451.  
  452.     Parameters : DLG standard
  453.     Called by  : Windows OS
  454.     Calls      : Nothing
  455.     Modifies   : Nothing
  456.     Returns    : Nothing
  457.  
  458. ***************************************************************************************************/
  459. #pragma argsused
  460. BOOL FAR PASCAL _export DLG_OptionsBox(HWND hDlg, UINT msg, WORD wParam, LONG lParam)
  461. {
  462.     switch(msg)
  463.     {
  464.     case WM_COMMAND:
  465.         switch(wParam)
  466.         {
  467.         case ST_DEMOBUTTON:
  468.             MessageBox(hDlg, "You have hit the test button", "Test Word", MB_ICONINFORMATION);
  469.             return FALSE;
  470.     }
  471.     }
  472. return FALSE;
  473. }
  474.